/**
 * 
 */
package com.alstudio.module.xmpp.protocol.xmpp.parser.iq.money;

import java.util.ArrayList;

import android.text.TextUtils;

import com.alstudio.module.xmpp.protocol.xmpp.ALXmppEventListener;
import com.alstudio.module.xmpp.protocol.xmpp.data.economic.EconomicRecords;
import com.alstudio.module.xmpp.protocol.xmpp.packact.ALIQ;
import com.alstudio.module.xmpp.protocol.xmpp.packact.IQNameSpace;
import com.alstudio.module.xmpp.protocol.xmpp.parser.iq.ALBaseIQParser;
import com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.IQXmlParseEventCallback;
import com.alstudio.utils.formater.numeric.ALNumericUtils;
import com.alstudio.utils.log.ALLog;

/**
 * 解析賬單或積分清單
 * 
 * @author alonso lee
 * 
 */
public class GetMoneyOrPointsHistoryParser extends ALBaseIQParser implements
		IQXmlParseEventCallback {

	private ArrayList<EconomicRecords> economicRecords;
	private boolean hasMore;

	@Override
	public void parseIQPackage(ALIQ packact, String stream,
			ALXmppEventListener listener) throws Exception {
		// TODO Auto-generated method stub
		code = 0;
		hasMore = false;
		mListener = listener;
		ns = packact.getNameSpace();
		economicRecords = null;
		economicRecords = new ArrayList<EconomicRecords>();
		startParse(packact, stream, this);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.
	 * IQXmlParseEventCallback#processErrorStartTag(java.lang.String)
	 */
	@Override
	public void processErrorStartTag(String tag) {
		// TODO Auto-generated method stub
		if (tag.equals(ERROR_CODE_TAG)) {
			code = ALNumericUtils.parseInt(getAttValue("code"), 0);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.
	 * IQXmlParseEventCallback#processSetStartTag(java.lang.String)
	 */
	@Override
	public void processSetStartTag(String tag) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.
	 * IQXmlParseEventCallback#processGetStartTag(java.lang.String)
	 */
	@Override
	public void processGetStartTag(String tag) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.
	 * IQXmlParseEventCallback#processResultStartTag(java.lang.String)
	 */
	@Override
	public void processResultStartTag(String tag) {
		// TODO Auto-generated method stub
		if (tag.equals(ITEMS_TAG)) {
			String tmp = getAttValue("more");
			if (!TextUtils.isEmpty(tmp) && tmp.equals(TRUE)) {
				hasMore = true;
			}
		} else if (tag.equals(ITEM_TAG)) {
			EconomicRecords r = new EconomicRecords();
			//时间戳
			r.setDate(ALNumericUtils.parseLong(getAttValue("date"),
					System.currentTimeMillis() / 1000));
			//类型
			r.setType(getAttValue("type"));
			//详细说明
			r.setDesc(getAttValue("desc"));
			//积分
			r.setPoints(getAttValue("points"));
			//金币
			r.setGold(getAttValue("gold"));
			economicRecords.add(r);
			ALLog.d("加入记录 " + r.getDate());
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.
	 * IQXmlParseEventCallback#processEndTag(java.lang.String)
	 */
	@Override
	public void processEndTag(String tag) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.
	 * IQXmlParseEventCallback#processStartDocument()
	 */
	@Override
	public void processStartDocument() {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.
	 * IQXmlParseEventCallback#processEndDocument()
	 */
	@Override
	public void processEndDocument() {
		// TODO Auto-generated method stub
		onCallback();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.ALBaseIQParser#onCallback
	 * ()
	 */
	@Override
	public void onCallback() {
		// TODO Auto-generated method stub
		if (mListener != null) {
			if (ns.equals(IQNameSpace.NS_GET_USER_MONEY_HISTORY)) {
				// 账单记录
				mListener.onGetEconomicRecordsResult(code, 0,hasMore,
						economicRecords);
				ALLog.d("回调账单记录 " + code);
			} else if (ns.equals(IQNameSpace.NS_GET_POINT_HISTORY)) {
				// 积分记录
				mListener.onGetEconomicRecordsResult(code, 1,hasMore,
						economicRecords);
				ALLog.d("回调积分记录 " + code);
			}
		}
	}

}
